//
//  sobelItAppDelegate.m
//
//  Created by Alexandre MOREL on 04/03/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "sobelItAppDelegate.h"
#import <OpenCV/cv.h>

@implementation sobelItAppDelegate
- (IBAction)myCalculer:(id)sender {
	
}

- (IBAction)myEnregistrer:(id)sender {
    
}

- (NSImage*)iplImageToNSImage:(IplImage*)iplImage {
	NSBitmapImageRep *bmp= [[NSBitmapImageRep alloc] 
							initWithBitmapDataPlanes:0
							pixelsWide:iplImage->width 
							pixelsHigh:iplImage->height 
							bitsPerSample:iplImage->depth 
							samplesPerPixel:iplImage->nChannels 
							hasAlpha:NO isPlanar:NO 
							colorSpaceName:NSDeviceRGBColorSpace 
							bytesPerRow:iplImage->widthStep 
							bitsPerPixel:0];
	NSUInteger val[3]= {0, 0, 0};
	for(int x=0; x < iplImage->width; x++) {
		for(int y=0; y < iplImage->height; y++) {
			CvScalar scal= cvGet2D(iplImage, y, x);
			val[0]= scal.val[0];
			val[1]= scal.val[1];
			val[2]= scal.val[2];
			[bmp setPixel:val atX:x y:y];
		}
	}
	NSImage *im= [[NSImage alloc] initWithData:[bmp TIFFRepresentation]];
	return [im autorelease];
}


- (IBAction)myOuvrir:(id)sender {
	[myProgress setHidden:(BOOL)false];
	[myProgress startAnimation: self]; 
    NSURL *imageURL = [NSURL URLWithString:@"http://www.almorel.org/wp-content/uploads/2011/03/P1000189.jpg"];
	NSData *imageData = [imageURL resourceDataUsingCache:NO];
	NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData];
	[myProgress stopAnimation: self]; 
	if (imageFromBundle)
	{
		// The image loaded properly, so lets display it.
		[myImage setImage:imageFromBundle];
		[myProgress setHidden:(BOOL)true];
		[imageFromBundle release];
	}
	else
	{
		NSLog(@"imageView could not be loaded.");
	}
}
@end
